package com.wfs.myapp.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA1Utils {

    public static String SHA1Example(String originalString) {
        try {
            // 获取SHA-1 MessageDigest 实例
            MessageDigest digest = MessageDigest.getInstance("SHA-1");

            // 对字符串进行编码
            byte[] encodedhash = digest.digest(originalString.getBytes("UTF-8"));

            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : encodedhash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
